package com.rech.xbexpress3;

import android.os.AsyncTask;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;


import java.util.List;


import com.bjw.bean.ComBean;
import com.bjw.utils.FuncUtil;
import com.bjw.utils.SerialHelper;
import com.rech.utils.CommandUtil;
import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Vector;


import static android.content.ContentValues.TAG;

public class SerialPortWorker{
    public static SerialPortHelper serialHelper;
    public static ArgObject serialObjectLock = new ArgObject();
    public static ArgObject serialRecvLock = new ArgObject();
    public static boolean seiralPortWriteWorking = false;
    public static boolean serialPortReadWorking = false;
    public static SerialPortReadWorker reader ;
    public static SerialPortWriteWorker writer;
};
class SerialPortWriteWorker extends  Thread{

    public SerialPortWriteWorker() {
        super();
        SerialPortWorker.seiralPortWriteWorking = true;


    }
    @Override
    public void run() {
        while(SerialPortWorker.seiralPortWriteWorking){
            try {
                synchronized (SerialPortWorker.serialObjectLock) {
                    SerialPortWorker.serialObjectLock.wait();
                    String arg = SerialPortWorker.serialObjectLock.getArg();
                    byte[] cmd = CommandUtil.hex2Bytes(arg);
                    //CommandUtil.hex2Bytes(arg);
                    SerialPortWorker.serialHelper.send(cmd);

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        SerialPortWorker.serialHelper.close();

    }


}


class SerialPortReadWorker extends Thread {

    public SerialPortReadWorker() {
        super();
        SerialPortWorker.serialPortReadWorking = true;
    }

    @Override
    public void run() {

        while (SerialPortWorker.serialPortReadWorking) {
            try {
                synchronized (SerialPortWorker.serialRecvLock) {
                    SerialPortWorker.serialRecvLock.wait();
                    String retCmd = SerialPortWorker.serialRecvLock.getArg();
                    try{
                        if(retCmd.length() > 11){

                            Message msg = new Message();
                            msg.what = 7777;
                            msg.obj = retCmd;
                            EventBus.getDefault().post(msg);

                            String funcCode = retCmd.substring(8, 10);
                            if (funcCode.equals("1a")) {
                                QueueService.recv1a(retCmd);
                            }
                            if (funcCode.equals("3a")) {
                                SerialPortWorker.serialHelper.sendHex(retCmd);
                                QueueService.recv3a(retCmd);
                            }
                            if (funcCode.equals("4a")) {
                                SerialPortWorker.serialHelper.sendHex(retCmd);
                                //TODO:返回开格子错误暂时当做已开格子
                                QueueService.recv4a(retCmd);
                            }
                            if (funcCode.equals("1c")) {
                                SerialPortWorker.serialHelper.sendHex(retCmd);
                            }
                        }

                    }catch (Exception e){
                        Log.d(TAG, retCmd);
                    }

                }
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
    }
}
class SerialPortHelper  extends SerialHelper {
    public SerialPortHelper(){
        super();
    }
    @Override
    protected void onDataReceived(ComBean comBean) {
        final ComBean ret = comBean;
        //2，重写run方法
        try {
            synchronized (SerialPortWorker.serialRecvLock) {
                String recvString = FuncUtil.ByteArrToHex(ret.bRec);
                recvString = recvString.toLowerCase();
                SerialPortWorker.serialRecvLock.setArg(recvString);
                SerialPortWorker.serialRecvLock.notify();
            }
        }catch (Exception e){

        }


    }
}